/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rukopisyimport;

import cz.mzk.rehan.commons.Dom4jUtils;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.joda.time.DateTime;
import rukopisyimport.foxml.InternalPart;
import cz.mzk.kramerius.imports.Monograph;
import rukopisyimport.foxml.Page;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class ConversionLog {

    private File logFile;
    private DateTime dateTime = new DateTime();
    private Map<String, File> dataSourceMap = new HashMap<String, File>();
    private ManuscriptCollection collection;
    private String alephBase;
    private String sysno;
    private String signature;
    private String monographPid;
    private String monographLabel;
    private boolean inImageServer;
    private List<Page> pageList;
    private List<InternalPart> internalPartList;

    public ConversionLog(File logFile) {
        this.logFile = logFile;
    }

    public void setCollection(ManuscriptCollection colection) {
        this.collection = colection;
    }

    public void setInImageServer(boolean inImageServer) {
        this.inImageServer = inImageServer;
    }

    public void logMonograph(Monograph monograph) {
        monographPid = monograph.getPid();
        monographLabel = monograph.getLabel();
        sysno = monograph.getSysno();
        signature = monograph.getSignature();
    }

    public void logDataSources(ManuscriptData manuscriptData) {
        File marcFile = manuscriptData.getMarcFile();
        dataSourceMap.put("marcxml", marcFile);
        File teiFile = manuscriptData.getTeiFile();
        dataSourceMap.put("tei_enrich", teiFile);
    }

    public void setPages(List<Page> pageList) {
        this.pageList = pageList;
    }

    public void setInternalParts(List<InternalPart> internalPartList) {
        this.internalPartList = internalPartList;
    }

    public void saveToFile() {
        Document doc = createDocument();
        Dom4jUtils.saveDocument(doc, logFile);
    }

    private Document createDocument() {
        Element manuscriptEl = DocumentHelper.createElement("manuscript");
        Element dateStampEl = manuscriptEl.addElement("datestamp");
        dateStampEl.addText(dateTime.toString());
        Element collectionEl = manuscriptEl.addElement("collection");
        collectionEl.addText(collection.getName());
        Element alephBaseEl = manuscriptEl.addElement("aleph-base");
        alephBaseEl.addText(alephBase);
        Element sysnoEl = manuscriptEl.addElement("sysno");
        sysnoEl.addText(sysno);
        Element signatureEl = manuscriptEl.addElement("signature");
        signatureEl.addText(signature);
        createDataSources(manuscriptEl);
        createMonograph(manuscriptEl);
        createPages(manuscriptEl);
        createInternalParts(manuscriptEl);
        return DocumentHelper.createDocument(manuscriptEl);
    }

    private void createDataSources(Element root) {
        Element dataSources = root.addElement("data_sources");
        for (String type : dataSourceMap.keySet()) {
            Element file = dataSources.addElement("file");
            file.addAttribute("type", type);
            file.addText(dataSourceMap.get(type).getAbsolutePath());
        }
    }

    private void createMonograph(Element manuscript) {
        Element monograph = manuscript.addElement("model_monograph");
        Element pid = monograph.addElement("pid");
        pid.addText(monographPid);
        Element label = monograph.addElement("label");
        label.addText(monographLabel);
    }

    private void createPages(Element manuscriptEl) {
        Element pages = manuscriptEl.addElement("pages");
        pages.addAttribute("inImageServer", Boolean.toString(inImageServer));
        for (Page page : pageList) {
            Element pageEl = pages.addElement("model_page");
            Element pid = pageEl.addElement("k4_pid");
            pid.addText(page.getPid());
            Element label = pageEl.addElement("foxml_label");
            label.addText(page.getLabel());
            Element index = pageEl.addElement("tei_surface_id");
            index.addText(page.getId());
        }
    }

    private void createInternalParts(Element manuscriptEl) {
        Element internalParts = manuscriptEl.addElement("internal_parts");
        for (InternalPart internalPart : internalPartList) {
            Element internalPartEl = internalParts.addElement("model_internal_part");
            Element pid = internalPartEl.addElement("pid");
            pid.addText(internalPart.getPid());
            Element label = internalPartEl.addElement("label");
            label.addText(internalPart.getLabel());
            addPages(internalPartEl, internalPart);
        }
    }

    private void addPages(Element internalPartEl, InternalPart internalPart) {
        Element pages = internalPartEl.addElement("pages");
        pages.addAttribute("firstPage", internalPart.getPageFrom());
        pages.addAttribute("lastIndex", internalPart.getPageTo());
        for (String pagePid : internalPart.getPagePidList()) {
            Element pid = pages.addElement("pid");
            pid.addText(pagePid);
        }
    }

    public void setAlephBase(String alephBase) {
        this.alephBase = alephBase;
    }
}
